package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitChallenge;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitWork;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.model.request.WorkIdRequest;
import com.graduation.springbootinit.service.OutfitChallengeService;
import com.graduation.springbootinit.service.OutfitWorkService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/outfit/challenge")
public class ActivityChallengeController {

    @Resource
    private OutfitChallengeService outfitChallengeService;
    
    @Resource
    private OutfitWorkService outfitWorkService;
    
    @Resource
    private UserService userService;

    // 管理员创建挑战
    @PostMapping("/add")
    public BaseResponse<Boolean> addChallenge(@RequestBody OutfitChallenge challenge, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        // TODO: 验证用户是否是管理员
        outfitChallengeService.createChallenge(challenge);
        return ResultUtils.success(true);
    }

    // 获取进行中的挑战
    @GetMapping("/ongoing")
    public BaseResponse<Page<OutfitChallenge>> getOngoingChallenges(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResultUtils.success(outfitChallengeService.getOngoingChallenges(pageNum, pageSize));
    }

    // 获取已结束的挑战
    @GetMapping("/ended")
    public BaseResponse<Page<OutfitChallenge>> getEndedChallenges(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResultUtils.success(outfitChallengeService.getEndedChallenges(pageNum, pageSize));
    }

    // 提交作品
    @PostMapping("/work/submit")
    public BaseResponse<Boolean> submitWork(@RequestBody OutfitWork work, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        work.setUserId(user.getId());
        outfitWorkService.submitWork(work);
        return ResultUtils.success(true);
    }

    // 获取挑战的作品列表
    @GetMapping("/work/list")
    public BaseResponse<Page<OutfitWork>> getWorks(
            @RequestParam Long challengeId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResultUtils.success(outfitWorkService.getWorksByChallenge(challengeId, pageNum, pageSize)
        );
    }

    // 点赞作品
    @PostMapping("/work/like")
    public BaseResponse<Boolean> likeWork(@RequestBody OutfitWork work, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        outfitWorkService.likeWork(work.getId(), user.getId());
        return ResultUtils.success(true);
    }

    // 取消点赞作品
    @PostMapping("/work/unlike")
    public BaseResponse<Boolean> unlikeWork(@RequestBody OutfitWork work, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        outfitWorkService.unlikeWork(work.getId(), user.getId());
        return ResultUtils.success(true);
    }

    // 获取用户的作品列表
    @GetMapping("/work/user")
    public BaseResponse<List<OutfitWork>> getUserWorks(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return ResultUtils.success(outfitWorkService.getWorksByUser(user.getId()));
    }

    // 管理员结束挑战并发放奖励
    @PostMapping("/end")
    public BaseResponse<Boolean> endChallenge(@RequestBody OutfitChallenge challenge, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        // TODO: 验证用户是否是管理员
        outfitChallengeService.endChallengeAndReward(challenge.getId());
        return ResultUtils.success(true);
    }

    // 根据作品ID获取作品详情
    @PostMapping("/work/get")
    public BaseResponse<OutfitWork> getWorkById(@RequestBody WorkIdRequest request) {
        OutfitWork work = outfitWorkService.getWorkById(request.getWorkId());
        return ResultUtils.success(work);
    }
} 